package com.xxxx.seckill.service_impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.mapper.OrderMapper;
import com.xxxx.seckill.pojo.Order;
import com.xxxx.seckill.pojo.SeckillGoods;
import com.xxxx.seckill.pojo.SeckillOrder;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IGoodsService;
import com.xxxx.seckill.service.IOrderService;
import com.xxxx.seckill.service.ISeckillGoodsService;
import com.xxxx.seckill.service.ISeckillOrderService;
import com.xxxx.seckill.utils.MD5Util;
import com.xxxx.seckill.utils.UUIDUtil;
import com.xxxx.seckill.vo.GoodsVo;
import com.xxxx.seckill.vo.OrderDetailVo;
import com.xxxx.seckill.vo.RespBean;
import com.xxxx.seckill.vo.RespBeanEnum;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author macaber
 * @since 2021-09-30
 */
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
         @Autowired
         ISeckillGoodsService iSeckillGoodsService;
         @Autowired
         OrderMapper orderMapper;
         @Autowired
         ISeckillOrderService iSeckillOrderService;
         @Autowired
    IGoodsService iGoodsService;
         @Autowired
    RedisTemplate redisTemplate;
    @Override
    public Order seckill(User user, GoodsVo goodsVoByGoodsId) {
        ValueOperations valueOperations = redisTemplate.opsForValue ( );
        SeckillGoods goods_id = iSeckillGoodsService.getOne (new QueryWrapper<SeckillGoods> ( ).eq ("goods_id", goodsVoByGoodsId.getId ()));
        goods_id.setStockCount (goods_id.getStockCount ()-1);

        boolean update = iSeckillGoodsService.update (new UpdateWrapper<SeckillGoods> ( ).setSql ("stock_count=stock_count-1")
                .eq ("goods_id", goods_id.getId ( )).gt ("stock_count", 0));

        if (goods_id.getStockCount ()<1){
//            判断是和否还有库存
              valueOperations.set ("isStockEmpty:"+goodsVoByGoodsId.getId (),"0");
            return null;
        }
        //        生成订单
        Order order = new Order ( );
        order.setUserId (user.getId ());
        order.setGoodsId (goodsVoByGoodsId.getId ());
        order.setDeliveryAddrId (0L);
        order.setGoodsName (goodsVoByGoodsId.getGoodsName ());
        order.setGoodsCount (1);
        order.setGoodsPrice (goods_id.getSeckillPrice ());
        order.setOrderChannel (1);
        order.setStatus (0);
        order.setCreateDate (new Date ());
        orderMapper.insert (order);
//        生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder ( );
        seckillOrder.setUserId (user.getId ());
        seckillOrder.setGoodsId (goodsVoByGoodsId.getId ());
        seckillOrder.setOrderId (order.getId ());
        iSeckillOrderService.save (seckillOrder);
        valueOperations.set ("order:"+user.getId ()+":"+goods_id.getId (),seckillOrder);
        return order;
    }

    @Override
    public OrderDetailVo detail(Long orderId) {
//        if(orderId==null){
//            return RespBean.error (RespBeanEnum.ORDER_NOT_EXIST);
//        }
        Order order = orderMapper.selectById (orderId);
        GoodsVo goodsVo = iGoodsService.findGoodsVoByGoodsId (order.getGoodsId ( ));
        OrderDetailVo detail = new OrderDetailVo ( );
        detail.setOrder (order);
        detail.setGoodsVo (goodsVo);
        return detail;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String str = MD5Util.md5 (UUIDUtil.uuid ( ) + "12345");
        redisTemplate.opsForValue ().set ("seckillPath:"+user.getId ()+":"+goodsId,str,60, TimeUnit.SECONDS);
        return str;
    }

    @Override
    public boolean checkPath(User user, Long goodsId, String path) {
        if (user==null||goodsId<0|| StringUtils.isEmpty (path)){
            return false;
        }
        String redisPath = (String) redisTemplate.opsForValue ( ).get ("seckillPath:" + user.getId ( ) + ":" + goodsId);
        return path.equals (redisPath);
    }

    @Override
    public boolean checkCaptcha(User user, Long goodsId, String captcha) {
        if (StringUtils.isEmpty (captcha)||user==null||goodsId<0){
            return  false;
        }
         String redisCaptcha= (String)redisTemplate.opsForValue ( ).get ("captcha:"+user.getId ()+":"+goodsId);

        return captcha.equals (redisCaptcha);
    }


}
